package com.ruoyi.kanban.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kanban.mapper.SupplyFrameworkItemMapper;
import com.ruoyi.kanban.domain.SupplyFrameworkItem;
import com.ruoyi.kanban.service.ISupplyFrameworkItemService;

/**
 * 供货框架物料明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
@Service
public class SupplyFrameworkItemServiceImpl extends ServiceImpl<SupplyFrameworkItemMapper, SupplyFrameworkItem> implements ISupplyFrameworkItemService
{
    @Autowired
    private SupplyFrameworkItemMapper supplyFrameworkItemMapper;

    /**
     * 查询供货框架物料明细
     * 
     * @param id 供货框架物料明细主键
     * @return 供货框架物料明细
     */
    @Override
    public SupplyFrameworkItem selectSupplyFrameworkItemById(Long id)
    {
        return supplyFrameworkItemMapper.selectSupplyFrameworkItemById(id);
    }

    /**
     * 查询供货框架物料明细列表
     * 
     * @param supplyFrameworkItem 供货框架物料明细
     * @return 供货框架物料明细
     */
    @Override
    public List<SupplyFrameworkItem> selectSupplyFrameworkItemList(SupplyFrameworkItem supplyFrameworkItem)
    {
        LambdaQueryWrapper<SupplyFrameworkItem> queryWrapper = new LambdaQueryWrapper<>();
        
        if (supplyFrameworkItem.getFrameworkId() != null) {
            queryWrapper.eq(SupplyFrameworkItem::getFrameworkId, supplyFrameworkItem.getFrameworkId());
        }
        if (StringUtils.isNotEmpty(supplyFrameworkItem.getFrameworkCode())) {
            queryWrapper.eq(SupplyFrameworkItem::getFrameworkCode, supplyFrameworkItem.getFrameworkCode());
        }
        if (StringUtils.isNotEmpty(supplyFrameworkItem.getMaterialCode())) {
            queryWrapper.like(SupplyFrameworkItem::getMaterialCode, supplyFrameworkItem.getMaterialCode());
        }
        if (StringUtils.isNotEmpty(supplyFrameworkItem.getMaterialName())) {
            queryWrapper.like(SupplyFrameworkItem::getMaterialName, supplyFrameworkItem.getMaterialName());
        }
        if (supplyFrameworkItem.getDelFlag() != null) {
            queryWrapper.eq(SupplyFrameworkItem::getDelFlag, supplyFrameworkItem.getDelFlag());
        } else {
            // 默认查询未删除的记录
            queryWrapper.eq(SupplyFrameworkItem::getDelFlag, 0);
        }
        
        return this.list(queryWrapper);
    }

    /**
     * 根据框架协议ID查询物料明细列表
     * 
     * @param frameworkId 框架协议ID
     * @return 供货框架物料明细集合
     */
    @Override
    public List<SupplyFrameworkItem> selectSupplyFrameworkItemByFrameworkId(Long frameworkId)
    {
        return supplyFrameworkItemMapper.selectSupplyFrameworkItemByFrameworkId(frameworkId);
    }

    /**
     * 新增供货框架物料明细
     * 
     * @param supplyFrameworkItem 供货框架物料明细
     * @return 结果
     */
    @Override
    public boolean insertSupplyFrameworkItem(SupplyFrameworkItem supplyFrameworkItem)
    {
        return this.save(supplyFrameworkItem);
    }

    /**
     * 修改供货框架物料明细
     * 
     * @param supplyFrameworkItem 供货框架物料明细
     * @return 结果
     */
    @Override
    public boolean updateSupplyFrameworkItem(SupplyFrameworkItem supplyFrameworkItem)
    {
        return this.updateById(supplyFrameworkItem);
    }

    /**
     * 批量删除供货框架物料明细
     * 
     * @param ids 需要删除的供货框架物料明细主键
     * @return 结果
     */
    @Override
    public boolean deleteSupplyFrameworkItemByIds(List<Long> ids)
    {
        return this.removeByIds(ids);
    }

    /**
     * 删除供货框架物料明细信息
     * 
     * @param id 供货框架物料明细主键
     * @return 结果
     */
    @Override
    public boolean deleteSupplyFrameworkItemById(Long id)
    {
        return this.removeById(id);
    }

    /**
     * 根据框架协议ID删除物料明细
     * 
     * @param frameworkId 框架协议ID
     * @return 结果
     */
    @Override
    public boolean deleteSupplyFrameworkItemByFrameworkId(Long frameworkId)
    {
        LambdaQueryWrapper<SupplyFrameworkItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SupplyFrameworkItem::getFrameworkId, frameworkId);
        return this.remove(queryWrapper);
    }
}